//
//  ST_QuickMessageContentItem.swift
//  btvideo
//
//  Created by mac on 2021/9/6.
//  Copyright © 2021 JarvanZhang. All rights reserved.
//

import UIKit
import RxSwift

class ST_QuickMessageContentItem: UICollectionViewCell {
    
    
    /// 删除按钮
    let delete_button: UIButton = {
        let button = UIButton(type: .system)
        button.setImage(#imageLiteral(resourceName: "quickMessage_icon_delete").original, for: .normal)
        return button
    }()
    
    /// 发送按钮
    let send_button: UIButton = {
        let button = UIButton(type: .system)
        button.setImage(#imageLiteral(resourceName: "quickMessage_icon_send_colorful").original, for: .normal)
        return button
    }()
    
    /// 尾部布局
    lazy var trailing_stack_view: UIStackView = {
        let stackView = UIStackView()
        stackView.axis = .horizontal
        stackView.distribution = .fill
        stackView.alignment = .center
        stackView.spacing = 10
        stackView.addArrangedSubviews([delete_button, send_button])
        return stackView
    }()
    
    /// 审核中
    let review_label: UILabel = {
        let label = UILabel()
        label.text = "[审核中]"
        label.textColor = UIColor(hexString: "#FEC207")
        label.font = .systemFont(ofSize: 15, weight: .regular)
        return label
    }()
    
    
    /// 文字内容
    let textLabel: UILabel = {
        let label = UILabel()
        label.text = "LabelLabelLabelLabelLabelLabelLabelLabelLabelLabel"
        label.textColor = UIColor(hexString: "#333333")
        label.font = .systemFont(ofSize: 15, weight: .regular)
        return label
    }()
    
    /// 语音内容
    let voice_button: UIButton = {
        let voice_button = UIButton(type: .system)
        voice_button.setImage(#imageLiteral(resourceName: "quickMessage_icon_voice3").original, for: .normal)
        voice_button.setTitle("1\"", for: .normal)
        voice_button.setTitleColor(.white, for: .normal)
        voice_button.contentHorizontalAlignment = .left
        voice_button.titleLabel?.font = .systemFont(ofSize: 16)
        voice_button.backgroundColor = UIColor(hexString: "#FEC207")
        voice_button.cornerRadius = 8
        voice_button.imageEdgeInsets = UIEdgeInsets(top: 0, left: 8, bottom: 0, right: 0)
        voice_button.titleEdgeInsets = UIEdgeInsets(top: 0, left: 12, bottom: 0, right: 0)
        voice_button.imageView?.animationImages = [#imageLiteral(resourceName: "quickMessage_icon_voice1"), #imageLiteral(resourceName: "quickMessage_icon_voice2"), #imageLiteral(resourceName: "quickMessage_icon_voice3")]
        voice_button.imageView?.animationDuration = 1
        voice_button.imageView?.animationRepeatCount = 0
        return voice_button
    }()
    
    /// 头部布局
    lazy var leading_stack_view: UIStackView = {
        let stackView = UIStackView()
        stackView.axis = .horizontal
        stackView.distribution = .fill
        stackView.alignment = .center
        stackView.spacing = 10
        stackView.addArrangedSubviews([review_label, textLabel, voice_button])
        return stackView
    }()
    
    /// 数字格式化
    let number_formatter: NumberFormatter = {
        let formatter = NumberFormatter()
        formatter.maximumFractionDigits = 2
        formatter.minimumFractionDigits = 0
        return formatter
    }()
    
    var disposeBag = DisposeBag()
    
    override func prepareForReuse() {
        super.prepareForReuse()
        
        disposeBag = DisposeBag()
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setup_views()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setup_views() {
        
        contentView.addSubview(trailing_stack_view)
        trailing_stack_view.snp.makeConstraints { make in
            make.centerY.equalToSuperview()
            make.trailing.equalToSuperview().offset(-10)
        }
        
        contentView.addSubview(leading_stack_view)
        leading_stack_view.snp.makeConstraints { make in
            make.centerY.equalToSuperview()
            make.leading.equalToSuperview().offset(10)
            make.trailing.lessThanOrEqualTo(trailing_stack_view.snp.leading).offset(-10)
        }
        
        
        voice_button.snp.makeConstraints { make in
            make.width.equalTo(80)
            make.height.equalTo(30)
        }
    }
    
    func configure(for model: ST_QuickMessageModel?, isEditing: Bool) {
        
        review_label.isHidden = model?.status != .waitingForReview
        textLabel.isHidden = model?.type != .text
        textLabel.text = model?.content
        voice_button.isHidden = model?.type != .voice
        voice_button.setTitle("\(number_formatter.string(from: NSNumber(floatLiteral: model?.duration ?? 0)) ?? "-")\"", for: .normal)
        if model?.status == .waitingForReview {
            delete_button.isHidden = !isEditing
            send_button.isHidden = true
        } else {
            delete_button.isHidden = !isEditing
            send_button.isHidden = isEditing
        }
    }
}
